import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable, catchError, map, of } from "rxjs";
import { PageInfo } from "../../sunshine-emp/emp.entity";

export interface Role {
    roleId: number;
    roleName: string;
    remark: string;
    createTime: number;
}

@Injectable({ providedIn: 'root' })
export class RoleService {
    httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        })
    };
    randomUserUrl = 'http://localhost:4200/wstro/admin/sys/role/list';
    // http://localhost:4200/wstro/admin/sys/login2
    getRoles(
        pageIndex: number,
        pageSize: number,
        sortField: string | null,
        sortOrder: string | null,
        filters: Array<{ key: string; value: string[] }>
    ): Observable<{ results: Role[], pageInfo: PageInfo }> {
        // this.http.get('http://localhost:4200/wstro/currentUser/perms').subscribe((result: any) => {
        //     console.log(result)
        // })
        let params = new HttpParams()
            .append('offset', `${(pageIndex - 1) * pageSize}`)
            .append('limit', `${pageSize}`)
            .append('sortField', `${sortField}`)
            .append('sortOrder', `${sortOrder}`);
        filters.forEach(filter => {
            filter.value.forEach(value => {
                params = params.append(filter.key, value);
            });
        });
        return this.http
            .get<{ results: Role[], pageInfo: PageInfo }>(`${this.randomUserUrl}`, { params })
            .pipe(map((res: any) => {
                console.log(res)
                return { results: res.page.list, pageInfo: res.pageInfo }
            }))

    }

    constructor(private http: HttpClient) { }
}